package demo;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Date;
import java.util.Scanner;

/**
 * 反射API
 *
 */
public class demo1 {

		public static void main(String[] args)
			throws InstantiationException, IllegalAccessException, ClassNotFoundException {
		// 动态加载类
		Scanner scan = new Scanner(System.in);
		System.out.println("请输入类名:");
		String className = scan.nextLine();

		Class<?> cls = null;
		cls = Class.forName(className);
		System.out.println("cls:" + cls);
		System.out.println("clsName:" + cls.getName());

		Object obj = cls.newInstance();
		System.out.println("obj:" + obj);

		// 动态检查类中声明的方法信息
		Method[] mds = cls.getDeclaredMethods();

		for (Method m : mds) {
			
			System.out.println(m.getName());
		}

		System.out.println("输入方法名:");
		String mdName = scan.nextLine();

		Method md;
		try {
		
			md = cls.getDeclaredMethod(mdName);
			Object val = md.invoke(obj);
			System.out.println(val);

		} catch (NoSuchMethodException e) {
			e.printStackTrace();
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}

	}

}
